package com.jadcon.checkbookregister.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Component;

import com.jadcon.checkbookregister.util.DbUtils;


@Component("entryRepository")
public class EntryRepositoryImpl implements EntryRepository{
	private SimpleJdbcTemplate simpleJdbcTemplate = DbUtils.getSimpleJdbcTemplate();

	public void save(Entry entry) {
		String sql = "insert into checkbook (checkNumber, name, amount) values (?,?,?)";
		simpleJdbcTemplate.update(sql, entry.getNumber(), entry.getName(), entry.getAmount());
	}
	
	public List<Entry> getList(){
		String sql = "select * from checkbook";
		return simpleJdbcTemplate.query(sql, new EntryMapper());
	}
	
	private final class EntryMapper implements RowMapper{

		public Entry mapRow(ResultSet rs, int rowNum) throws SQLException {
			String checkNumber = rs.getString("checkNumber");
			String name = rs.getString("name");
			String amount = rs.getString("amount");
			Entry entry = new Entry();
			entry.setAmount(amount);
			entry.setName(name);
			entry.setNumber(checkNumber);
			return entry;
		}
		
	}

}
